package flc.ast.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.SeekBar;

/**
 * 四挡开关
 * by longzhibin on 2023/10/16.
 */

public class SwitchSeekBar extends androidx.appcompat.widget.AppCompatSeekBar implements SeekBar.OnSeekBarChangeListener {


    private SeekTouchListener touchListener;


    /**
     * 新的Progress值,初始值是5 为了解决左右显示不全问题
     */
    private int newProgress = 0;


    public SwitchSeekBar(Context context) {
        super(context);
        setOnSeekBarChangeListener(this);
        setProgress(0);//四挡开关,为了解决thumb显示不全，所以设为5
    }

    public SwitchSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnSeekBarChangeListener(this);
        setProgress(0);
    }

    public SwitchSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnSeekBarChangeListener(this);
        setProgress(0);
    }

    /**
     * 进度值改变时,滑动的时候设置进度值
     *
     * @param seekBar
     * @param progress
     * @param fromUser
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        newProgress = progress;

    }

    /**
     * 进度值开始滑动时
     *
     * @param seekBar
     */
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    /**
     * 进度值停止滑动时
     *
     * @param seekBar
     */
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

        if (newProgress < 17) {//如果progress<17,则滑到第一档
            newProgress = 0;
            setProgress(0);
            if (touchListener != null) {
                touchListener.touchTop(seekBar);
            }

        } else if (newProgress >= 17 && newProgress < 50) {
            newProgress = 33;
            setProgress(33);
            if (touchListener != null) {
                touchListener.touchSecond(seekBar);
            }

        } else if (newProgress >= 50 && newProgress < 79) {
            newProgress = 66;
            setProgress(66);
            if (touchListener != null) {
                touchListener.touchThird(seekBar);
            }

        } else {
            newProgress = 100;
            setProgress(100);
            if (touchListener != null) {
                touchListener.touchEnd(seekBar);
            }
        }


    }

    /**
     * 滑动监听
     */
    public interface SeekTouchListener {
        void touchTop(SeekBar seekBar);//滑到第一档

        void touchSecond(SeekBar seekBar);//滑到第二档

        void touchThird(SeekBar seekBar);//滑到第三档

        void touchEnd(SeekBar seekBar);//滑到第四档

    }


    public void setSeekTouchListenr(SeekTouchListener touchListenr) {
        this.touchListener = touchListenr;

    }


}
